<?php

namespace app\app\controller;

use app\app\model\MaterialInventoryListModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class MaterialInventoryList extends BaseCurd
{
    public function initialize()
    {
        $this->model = MaterialInventoryListModel::class;
    }

    /**
     * C&U
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new $this->model();
        } else {
            $model = $this->model::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }

    /**
     * R
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $where = [];

        // 搜索
        $number = input('number', '');
        if (!empty($number)) $where[] = ['a.number', 'like', '%' . $number . '%'];

        $describe = input('describe', '');
        if (!empty($describe)) $where[] = ['a.describe', 'like', '%' . $describe . '%'];

        $status = input('status', '');
        if (!empty($status)) $where[] = ['a.status', 'like', '%' . $status . '%'];

        $supplier = input('supplier', '');
        if (!empty($supplier)) $where[] = ['a.supplier', 'like', '%' . $supplier . '%'];

        $project_name = input('project_name', '');
        if (!empty($project_name)) $where[] = ['a.project_name', 'like', '%' . $project_name . '%'];

        $page_size = input('pageSize', '20');

        $data = $this->model::where($where)
            ->alias('a')
            ->leftJoin('t_project b', 'a.project_id = b.id')
//            ->field(['a.*', 'b.project_name']);
            ->field(['a.*']);

//        $project_name = input('project_name', '');
//        if (!empty($project_name)) {
//            $data->where('b.project_name', 'like', '%' . $project_name . '%');
//        }

        $data = $data->paginate($page_size);

        empty($data) && $this->error('查询为空');

        $this->success('success', $data->toArray());
    }

    /**
     * detail
     * @param int $id
     * @return void
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function detail($id = 0)
    {
        !$id && $this->error('请传入id');

        $data = $this->model::where('id', $id)
            ->find();
        empty($data) && $this->error('查询为空');
        $data = $data->toArray();

        if (!empty($data['project_id'])) {
            $children_project = Db::table('t_project')
                ->where('id', $data['project_id'])
                ->find();
            $project = Db::table('t_project')
                ->where('id', $children_project['link_project_id'])
                ->find();

            $data['children_project_name'] = $children_project['project_name'];
            $data['project_name'] = $project['project_name'];
        }

        $this->success('success', $data);
    }

    /**
     * D
     * @return void
     * @throws DbException
     */
    public function del($id = 0)
    {
        !$id && $this->error('请传入id');

        $this->model::where('id', $id)->delete();

        $this->success('success');
    }
}